<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 环形子数组的最大和 -->
    <script>
      var maxSubarraySumCircular = function (nums) {
        //dp1数组记录以nums[i]结尾的连续子数组的最大和
        //dp2数组记录以nums[i]结尾连续子数组的最小和
        let dp1 = [nums[0]],
          dp2 = [nums[0]]
        //total表示数组的总和
        let total = nums[0]
        //res1 记录 dp1 的最大值 res2 记录 dp2 的最小值
        let res1 = nums[0],
          res2 = nums[0]
        for (let i = 1; i < nums.length; i++) {
          dp1[i] = Math.max(dp1[i - 1] + nums[i], nums[i])
          dp2[i] = Math.min(dp2[i - 1] + nums[i], nums[i])
          res1 = Math.max(res1, dp1[i])
          res2 = Math.min(dp2[i], res2)

          total += nums[i]
        }
        return total == res2 ? res1 : Math.max(res1, total - res2)
      }
      console.log([1, -2, 3, -2])
    </script>
  </body>
</html>
